//
//  PictureBrowserPresentationController.swift
//  SwiftWB
//
//  Created by 钟清 on 17/4/9.
//  Copyright © 2017年 Zhong Qing. All rights reserved.
//

import UIKit

protocol PictureBrowserPresentationDelegate: NSObjectProtocol {
    /* 创建和点击图片一样的UIImageView */
    func browserPresentationWillShowImageView(browserPresentationController: PictureBrowserPresentationController,
                                                                  indexPath: NSIndexPath) -> UIImageView
    /* 获取点击图片相对于keywindow的frame */
    func browserPresentationWillFromFrame(browserPresentationController: PictureBrowserPresentationController,
                                                              indexPath: NSIndexPath) -> CGRect
    /* 获取点击图片最终的frame */
    func browserPresentationWillToFrame(browserPresentationController: PictureBrowserPresentationController,
                                                            indexPath: NSIndexPath) -> CGRect
}

class PictureBrowserPresentationController: UIPresentationController,
                                            UIViewControllerTransitioningDelegate,
                                            UIViewControllerAnimatedTransitioning {
    /* 标记，当前是否展示 */
    private var isPresent = false
    /* 图片对应的索引 */
    private var index: NSIndexPath?
    /* 代理对象 */
    weak var browserDelegate: PictureBrowserPresentationDelegate?
    /* 设置默认数据 */
    func setDefaultInfo(index: NSIndexPath, browserDelegate: PictureBrowserPresentationDelegate) {
        self.index = index
        self.browserDelegate = browserDelegate
    }
    // MARK: - UIViewControllerTransitionDelegate
    /* 返回负责转场动画的对象 */
    func presentationControllerForPresentedViewController(presented: UIViewController,
                                presentingViewController presenting: UIViewController,
                                        sourceViewController source: UIViewController) -> UIPresentationController? {
        
        return PictureBrowserPresentationController(presentedViewController: presented,
                                                   presentingViewController: presenting)
    }
    /* 返回一个负责转场动画中如何出现的对象 */
    func animationControllerForPresentedController(presented: UIViewController,
                             presentingController presenting: UIViewController, sourceController
                                                      source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        isPresent = true
        return self
    }
    /* 返回一个负责转场动画中如何消失的对象 */
    func animationControllerForDismissedController(dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        isPresent = true
        return self
    }
    // MARK: - UIViewControllerAnimatedTransitioning
    /* 转场动画展现和消失的动画时长 */
    func transitionDuration(transitionContext: UIViewControllerContextTransitioning?) -> NSTimeInterval {
        return 0.5
    }
    /* 自定义，modald的展现和消失 */
    func animateTransition(transitionContext: UIViewControllerContextTransitioning) {
        if isPresent { // 展现
            willPresentedController(transitionContext)
        } else { // 消失
            willDismissedController(transitionContext)
        }
    }
    /* 执行展现动画 */
    private func willPresentedController(transitionContext: UIViewControllerContextTransitioning) {
        assert(index != nil, "必需设置被点击cell的indexPath")
        assert(browserDelegate != nil, "必须设置代理才能展现")
        guard let toView = transitionContext.viewForKey(UITransitionContextToViewKey) else {
            return
        }
        let imageView = browserDelegate!.browserPresentationWillShowImageView(self, indexPath: index!)
        imageView.frame = browserDelegate!.browserPresentationWillFromFrame(self, indexPath: index!)
        transitionContext.containerView()!.addSubview(imageView)
        
        let toFrame = browserDelegate!.browserPresentationWillToFrame(self, indexPath: index!)
        UIView.animateWithDuration(transitionDuration(transitionContext), animations: {
            imageView.frame = toFrame
            }) { (_) in
                imageView.removeFromSuperview()
                transitionContext.containerView()!.addSubview(toView)
                transitionContext.completeTransition(true)
        }
    }
    /* 执行消失动画 */
    private func willDismissedController(transitionContext: UIViewControllerContextTransitioning) {
        transitionContext.completeTransition(true)
    }
}
